package com.songyang.mqttandothers.controller;

import com.songyang.mqttandothers.bean.UserBean;
import com.songyang.mqttandothers.common.ResultMapUtil;
import com.songyang.mqttandothers.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/auth")
public class authController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Object login(@RequestBody UserBean user, HttpServletRequest request) {
        Integer userId = userService.login(user);
        log.info("userId:" + userId);
        if (userId != -1) return ResultMapUtil.getHashMapLogin("登录成功", "1", userId);
        else return ResultMapUtil.getHashMapLogin("登录失败", "2", null);
    }

    @PostMapping("/register")
    public Object register(@RequestBody UserBean user, String checkCode) {
        boolean result = userService.register(user);
        if (result) return ResultMapUtil.success();
        else return ResultMapUtil.fail("失败");
    }
}
